$(document).ready(function() {
	refresh();
	setInterval('refresh()',10000);
});

refresh = function() {
	$.post('/controller/listevents?tumblelogId=' + $('#tumblelogId').val(),{},function(data) {
    	update(data);
    });
}

update = function(data) {
	if (data == "NONE") {
		$('div#wait').show();
		$('#tagline').hide();
	} else if (data == "REDIRECT") {
     	window.location = "/";
    } else {
    	$('div#wait').hide();
    	$('#tagline').show();
    	var offset = new Date().getTimezoneOffset() * 60 * 1000;
    	var response = eval(data);
		for (var i = 0, len = response.length; i < len; i++) {
			if ($('#' + response[i].id).length == 0) {
				var serverDate = Date.parse(response[i].date);
				var clientDate = serverDate - offset;
				var displayDate = dateFormat(clientDate,'HH:MM ddd d mmmm yyyy');
				$('#eventlist').prepend(
					'<li class="follower" id="' + response[i].id + '">' + 
					'<a id="followerlink" href="' + response[i].url + '">' +
					'<img src="' + response[i].thumbnail + '"/>' +
					'<span id="eventname">' + response[i].name + '</span>' +
					'<br/>' +
					'<span id="eventmessage">' + response[i].action + ' following ' + response[i].followed + '</span>' +
					'<br/>' +
					'<span id="eventdate">' + displayDate + '</span>' +
					'</a>' +
					'</span>' +
					'</li>'
				);
				$('#followercount').html(response[i].size);
			}
		}
	}
}